package com.lepu.app.fun.chat.business.chatcore;

import android.database.Cursor;

import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.fun.chat.bean.ChatSession;
import com.lepu.app.fun.chat.util.DbQueryRunner;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import util.TimeUtil;

/**
 * Created by Administrator on 2015/12/11.
 * 问题dao
 */
public class QuestionDao {

    private static final String TAG = QuestionDao.class.getSimpleName();

    static class QuestionDaoHolder {
        static QuestionDao chatDAO = new QuestionDao();
    }

    public static QuestionDao getInstance() {
        return QuestionDaoHolder.chatDAO;
    }


    public ChatSession getChatSessionById(String id) {
        try {
            String sql = "SELECT * from chat_Session  where QuestionID = ?";
            String[] params = new String[]{id};
            List<ChatSession> ids = getSessionsBySql(sql, params);
            if (ids.isEmpty()) {
                return null;
            } else {
                return ids.get(0);
            }
        } catch (Exception e) {
            LogUtilBase.LogE(TAG, e.toString());
        }
        return new ChatSession();
    }


    public List<ChatSession> getSessionsBySql(String sql, String[] params) {
        try {
            DbQueryRunner queryRunner = DbQueryRunner.getInstance();

            List<ChatSession> sessions = queryRunner.query(sql, new DbQueryRunner.RowHandler() {
                @Override
                public Object handle(Cursor c) {
                    ChatSession chat = new ChatSession();

                    chat.setSessionId(c.getInt(c.getColumnIndex("SessionId")));
                    chat.setGender(c.getInt(c.getColumnIndex("Gender")));
                    chat.setNewMsgCount(c.getInt(c.getColumnIndex("NewMsgCount")));

                    chat.setHostUserId(c.getLong(c.getColumnIndex("HostUserId")));
                    chat.setLastMsgContent(c.getString(c.getColumnIndex("LastMsgContent")));
                    chat.setUserId(c.getLong(c.getColumnIndex("UserId")));
                    chat.setUserNickName(c.getString(c.getColumnIndex("UserNickName")));
                    chat.setUserProfilePictureId(c.getString(c.getColumnIndex("UserProfilePictureId")));
                    chat.setQuestionID(c.getString(c.getColumnIndex("QuestionID")));
                    //// TODO: 2015/12/10 新添加的字段
                    chat.setQuestion(c.getString(c.getColumnIndex("Question")));
                    chat.setIsClosed(c.getString(c.getColumnIndex("IsClosed")));
                    chat.setRecordCreateTime(c.getString(c.getColumnIndex("RecordCreateTime")));


                    if (!c.isNull(c.getColumnIndex("LastMsgTime"))) {
                        Date dateTime = TimeUtil.getUtcDateTime(c.getString(c.getColumnIndex("LastMsgTime")));
                        chat.setLastMsgTime(dateTime);
                    }


                    return chat;
                }
            }, params);

            if (sessions != null) {
                LogUtilBase.LogD(TAG, "getSessionsBySql return " + sessions.size() + " records");
            } else {
                LogUtilBase.LogD(TAG, "getSessionsBySql return null!");
            }

            return sessions;
        } catch (Exception e) {
            LogUtilBase.LogE(TAG, e.toString());
        }
        return null;
    }

    /**
     * 更新问题
     *
     * @param session
     */
    public void updateChatSession(ChatSession session) {
        try {
            DbQueryRunner queryRunner = DbQueryRunner.getInstance();
            String sql = "UPDATE chat_Session SET " +
                    "NewMsgCount=?,LastMsgContent=?,LastMsgTime=?," +
                    "UserProfilePictureId=?,UserNickName=?,Question=?,IsClosed=?,RecordCreateTime=?" +
                    "WHERE HostUserId=? AND QuestionID=?";

            Object[] params = new Object[]{
                    session.getNewMsgCount(),
                    session.getLastMsgContent(),
                    TimeUtil.formatToUtcDateTime(session.getLastMsgTime()),
                    session.getUserProfilePictureId(),
                    session.getUserNickName(),
                    session.getQuestion(),
                    session.getIsClosed(),
                    session.getRecordCreateTime(),
                    session.getHostUserId(),
                    session.getQuestionID()};

            queryRunner.update(sql, params);

        } catch (Exception e) {
            LogUtilBase.LogE(TAG, e.toString());
        }
    }

}
